package zjut.raymond.witdemo.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;

import android.bluetooth.BluetoothGatt;
import android.graphics.PointF;
import android.os.Handler;

public class MyViewModel extends ViewModel {
    //连接设备的Mac地址
    private MutableLiveData<String> deviceMac;
    //扫描的设备列表
    private MutableLiveData<ArrayList<String>> deviceList;
    private ArrayList<String> devices = new ArrayList<>();
    //设备gatt
    private MutableLiveData<BluetoothGatt> gattMutableLiveData;

    private MutableLiveData<PointF> pdrLocationResult = null;

    private MutableLiveData<Boolean> booleanRecord;
    private MutableLiveData<Boolean> isPDRInit;

    private MutableLiveData<Integer> stepCount;

    //获取已连接设备的Mac地址
    public MutableLiveData<String> getDeviceMac() {
        if (deviceMac == null) {
            deviceMac = new MutableLiveData<>();
            deviceMac.setValue("null");
        }
        return deviceMac;
    }

    //设置已连接设备的Mac地址
    public void setDeviceMac(String deviceMac) {
        getDeviceMac().setValue(deviceMac);
    }

    public MutableLiveData<ArrayList<String>> getDeviceList() {
        if (deviceList == null) {
            deviceList = new MutableLiveData<>();
            loadData();
        }
        return deviceList;
    }

    private void loadData() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                deviceList.postValue(devices);
            }
        }, 1000);
    }

    public void addScanDeviceMac(String mac) {
        if (!devices.contains(mac)) {
            devices.add(mac);
            deviceList.postValue(devices);
        }
    }

    public MutableLiveData<PointF> getPdrLocationResult() {
        if (pdrLocationResult == null) {
            pdrLocationResult = new MutableLiveData<>();
        }
        return pdrLocationResult;
    }

    public void setPdrLocationResult(PointF pdrLocationResult) {
        getPdrLocationResult().setValue(pdrLocationResult);
    }

    public MutableLiveData<Boolean> getIsPDRInit() {
        if (null == isPDRInit) {
            isPDRInit = new MutableLiveData<>();
            isPDRInit.setValue(false);
        }
        return isPDRInit;
    }

    public void setIsPDRInit(boolean isPDRInit) {
        getIsPDRInit().postValue(isPDRInit);
    }

    public MutableLiveData<Boolean> getBooleanRecord() {
        if (null == booleanRecord) {
            booleanRecord = new MutableLiveData<>();
            booleanRecord.postValue(false);
        }
        return booleanRecord;
    }

    public void setBooleanRecord(boolean record) {
        getBooleanRecord().postValue(record);
    }

    //步数计算

    public MutableLiveData<Integer> getStepCount() {
        if (stepCount == null) {
            stepCount = new MutableLiveData<>();
            stepCount.postValue(0);
        }
        return stepCount;
    }

    public void addStepCount() {
        stepCount.postValue(getStepCount().getValue() + 1);
    }

    public void clearStepCount() {
        stepCount.postValue(0);
    }

    //WiFi定位
    private MutableLiveData<PointF> wifiLocation;

    public MutableLiveData<PointF> getWifiLocation() {
        if (wifiLocation == null) {
            wifiLocation = new MutableLiveData<>();
            wifiLocation.postValue(new PointF(0,0));
        }
        return wifiLocation;
    }
}
